import sys
import pandas as pd
import requests
from bs4 import BeautifulSoup

# 请求头可写，但我看没报错，就没写
headers = {
    'user-agent': '',
    'Cookie': ''
}

# 列表划分，例如[1,2,3,4]=>[[1,2],[3,4]],目的适应pyecharts数据
def chunk_list(lst, size):
    return [lst[i:i + size] for i in range(0, len(lst), size)]


# 数据图一的部分数据列表
def data1(soup):
    seven_dayList = [i.text.strip().replace(' ', '').replace('\n', '') for i in soup.select('.day-item')]
    temp = []
    # 数据清洗
    j = 0
    for i in seven_dayList:
        if j % 10 == 0:
            temp.append('2023年' + i[3:5] + '月' + i[6:] + '日')
            temp.append(i[:3])
        if j % 10 == 5:
            temp.append(i[:3])
            temp.append(i[3:])
        if i != '' and j % 10 != 0 and j % 10 != 5:
            temp.append(i)
        j += 1
    # 数据格式对应表格
    seven_dayLists = chunk_list(temp, 10)
    return seven_dayLists


# 图二表格数据列表
def data2(soup):
    total_list = [i.text.strip() for i in soup.select('.hour-table td')]
    temp = []
    for i in total_list:
        if i != '' and i != '天气':
            temp.append(i)
    # 时间
    time_list = []
    # 气温
    temperature_list = []
    # 降水
    rainfall_list = []
    # 风速
    windspeed_list = []
    # 风向
    winddirection_list = []
    # 气压
    pressure_list = []
    # 湿度
    humidity_list = []
    # 云量
    cloud_list = []
    categories = ['时间', '气温', '降水', '风速', '风向', '气压', '湿度', '云量']
    current_category = None
    for item in temp:
        if item in categories:
            current_category = item
        else:
            if current_category == '时间':
                time_list.append(item)
            elif current_category == '气温':
                temperature_list.append(item)
            elif current_category == '降水':
                rainfall_list.append(item)
            elif current_category == '风速':
                windspeed_list.append(item)
            elif current_category == '风向':
                winddirection_list.append(item)
            elif current_category == '气压':
                pressure_list.append(item)
            elif current_category == '湿度':
                humidity_list.append(item)
            elif current_category == '云量':
                cloud_list.append(item)
    # 切割
    return chunk_list(time_list, 8), chunk_list(temperature_list, 8), chunk_list(rainfall_list, 8), \
           chunk_list(windspeed_list, 8), chunk_list(winddirection_list, 8), chunk_list(pressure_list, 8), \
           chunk_list(humidity_list, 8), chunk_list(cloud_list, 8)


# 数据转换(表格所需)
def data_change(seven_dayList, temperature_list, windspeed_list, pressure_list, humidity_list, cloud_list):
    # 将seven_dayList切出最高气温和最低气温
    data_list = []
    high_temperature = []
    low_temperature = []
    for i in seven_dayList:
        data_list.append(i[0])
        high_temperature.append(eval(i[5][:-1]))
        low_temperature.append(eval(i[6][:-1]))
    # 将temperature_list转化为数字
    temperature_lists = []
    for i in temperature_list:
        temperature_lists.append(eval(i[:-1]))
    # 将windspeed_list转化为数字
    windspeed_lists = []
    for i in windspeed_list:
        windspeed_lists.append(eval(i[:-3]))
    # 将pressure_list转化为数字
    pressure_lists = []
    for i in pressure_list:
        pressure_lists.append(eval(i[:-3]))
    # 将humidity_list转化为数字
    humidity_lists = []
    for i in humidity_list:
        humidity_lists.append(round(eval(i[:-1]) / 100, 3))
    # 将cloud_list转化为数字
    cloud_lists = []
    for i in cloud_list:
        cloud_lists.append(eval(i[:-1]))
    return data_list, high_temperature, low_temperature, temperature_lists, \
           windspeed_lists, pressure_lists, humidity_lists, cloud_lists

# 关键代码（通过城市名字找到cityinfo.xls文件对应url后的对应代码）
def weather_data(city):
    try:
        data = pd.read_excel('F:\PythonXM\WeatherXM\static\cityinfo.xls', index_col='城市名称')
        code = data.loc[city]['对应代码']
    except Exception as e:
        print('输入的城市错误，请重新输入！')
        print(e)
        sys.exit()
    # 对应城市的url地址
    url = 'https://weather.cma.cn/web/weather/{}.html'.format(code)
    res = requests.get(url=url, headers=headers).content.decode('utf-8')
    soup = BeautifulSoup(res, 'lxml')
    seven_dayList = data1(soup)
    time_list, temperature_list, rainfall_list, windspeed_list, winddirection_list, \
    pressure_list, humidity_list, cloud_list = data2(soup)

    # 测试数据（上面是所有数据可自行选取，清洗）

    # print(seven_dayList)
    # print(time_list)
    # print(temperature_list)
    # print(rainfall_list)
    # print(windspeed_list)
    # print(winddirection_list)
    # print(pressure_list)
    # print(humidity_list)
    # print(cloud_list)

    # 只取当日数据和所需表格数据
    data_list, high_temperature, low_temperature, temperature_lists, \
    windspeed_lists, pressure_lists, humidity_lists, cloud_lists = \
        data_change(seven_dayList, temperature_list[0], windspeed_list[0],
                    pressure_list[0], humidity_list[0], cloud_list[0])

    # 测试数据
    # print(data_list)
    # print(time_list)
    # print(high_temperature)
    # print(low_temperature)
    # print(temperature_lists)
    # print(windspeed_lists)
    # print(pressure_lists)
    # print(humidity_lists)
    # print(cloud_lists)

    return seven_dayList, data_list, high_temperature, low_temperature, time_list[0], temperature_lists, \
           windspeed_lists, pressure_lists, humidity_lists, cloud_lists


if __name__ == '__main__':
    weather_data('张家界')
